home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / oopic.cpp < prev    next >
C/C++ Source or Header  |  1994-11-02  |  14KB  |  679 lines

  1. #include "oopic.h"
  2. #include <string.h>
  3. #include "nret.h"
  4. #include <dir.h>
  5. #include <errno.h>
  6.  
  7. void  OOPic::interprete()               // Overloads Slang::interprete()
  8.     {                                   // Some things, like error processing
  9.     int oldtok;                         // differs from Slang:: version
  10.     kh_error_code = KH_SUCCESS;
  11.     do {                                // because this one do not use exit()
  12.     terminate();
  13.     if(error)                  // Error detected
  14.         {
  15.         if(sub_used > 0)
  16.         {
  17.         prog = sub_pop();
  18.         return;
  19.         }
  20.         while(play_used > 1)   // We need clear all nested programs and
  21.         delete playpop();  // "program" itself.
  22.         delete program;
  23.         program = NULL;
  24.         delete variables;            // We also clears all variables
  25.         variables = new VarTable();  // and initialize them again.
  26.         return;
  27.         }
  28.     token_type = get_token();     // Get next lexem
  29.     if(token_type == VARIABLE)    // If it is name (only x = ... or
  30.         {                         // a[12]... recognized
  31.         putback();                // Put it back to input stream and
  32.         assigment();              // make assignment.
  33.         }
  34.     else
  35.         switch(oldtok = tok)  // If it is operator or subroutine call
  36.         {
  37.         case GOTO:     slang_goto(); break;   // This block is the
  38.         case LABEL:    find_eol(); break;     // same as in Slang
  39.         case GOSUB:    gosub();    break;     // class. It realize
  40.         case PLAYEX:     play();     break;   // language facilities.
  41.         case PRINT:    print();    break;
  42.         case IF:       slang_if();  break;
  43.         case FOR:      slang_for(); break;
  44.         case NEXT:     next();     break;
  45.         case INPUT:    input();    break;
  46.         case RETURN:   sub_return();  break;
  47.         case PAUSE:    pause(); break;
  48.  
  49.         case LINE: Line(); break;            // This block contain
  50.         case LINETO: lineTo(); break;        // painting perators
  51.         case ELLIPSE: Ellipse(); break;      // which are specific
  52.         case RECTANGLE: Rectangle(); break;  // for OOPic class.
  53.         case POLY: Poly(); break;
  54.         case TXT: text(); break;
  55.  
  56.         case ZOOM: zoom_picture(); break;
  57.         case ADDZOOM: add_zoom_picture(); break;
  58.         case SCROLL: scroll(); break;
  59.         case ADDSCROLL: add_Scroll(); break;
  60.         case MIRROR: Mirror(); break;
  61.         case ROTATE_ON: set_stack(ON); break;
  62.         case ROTATE_OFF: set_stack(OFF); break;
  63.         case ENDROTATE: r_stack->pop();  break;
  64.  
  65.         case SETCOLOR: setcolor(); break;
  66.         case SETCOLOR_RGB: setcolor_RGB(); break;
  67.  
  68.         case SETFILLSTYLE: setfillstyle(); break;
  69.         case SETTEXTSIZE: settextsize(); break;
  70.         case SETFONT: setfont(); break;
  71.         case SETFILL: setfill(); break;
  72.         case MOVETO: moveTo(); break;
  73.         case ROTATE: rotate(); break;
  74.         case SETLINE: setline(); break;
  75.  
  76.         case END:                 // End of "main" or played program.
  77.             if(play_used == 1)    // If it is main program, then
  78.             return;           // no action is taken.
  79.  
  80.             delete playpop();     // If we play external file, pop
  81.                       // it from program stack, and
  82.             delete program;
  83.             play_stack* p_s;      // Then we load previous file
  84.             program = load_program((p_s = playpop())->prog);
  85.             prog = program + p_s->shift;
  86.             delete p_s;
  87.             scan_labels();        // And scan it for labels
  88.             break;
  89.         }
  90.     } while((oldtok != FINISHED ) && (oldtok != RETURN));
  91.     }
  92. //////////////////////
  93. void OOPic::rotate()
  94.     {
  95.     if(error)
  96.     return;
  97.  
  98.     double a, x, y;
  99.     get_token();    // "("
  100.     if(*token != '(')
  101.     {
  102.     serror(1);
  103.     return;
  104.     }
  105.     if(get_argument(&a) || get_argument(&x) || get_argument(&y))
  106.     {
  107.     serror(0);
  108.     return;
  109.     }
  110.     alpha = (int)(a);
  111.     center.X = (int)x;
  112.     center.Y = (int)y;
  113.  
  114.     Paint::rotate(center, alpha);
  115.     }
  116. /////////////////////
  117. void OOPic::zoom_picture()
  118.     {
  119.     if(error)
  120.     return;
  121.  
  122.     double zx, zy;
  123.     get_token();    // "("
  124.     if(*token != '(')
  125.     {
  126.     serror(1);
  127.     return;
  128.     }
  129.     if(get_argument(&zx) || get_argument(&zy) || zx <= 0 || zy <= 0)
  130.     {
  131.     serror(0);
  132.     return;
  133.     }
  134.  
  135.     Paint::set_zoom(zx, zy);
  136.     }
  137. //////////////////////
  138. void OOPic::add_zoom_picture()
  139.     {
  140.     if(error)
  141.     return;
  142.  
  143.     double zx, zy;
  144.     get_token();    // "("
  145.     if(*token != '(')
  146.     {
  147.     serror(1);
  148.     return;
  149.     }
  150.     if(get_argument(&zx) || get_argument(&zy) || zx <= 0 || zy <= 0)
  151.     {
  152.     serror(0);
  153.     return;
  154.     }
  155.     Paint::set_add_zoom(zx, zy);
  156.     }
  157. //////////////////////
  158. void OOPic::setline()
  159.     {
  160.     if(error)
  161.     return;
  162.  
  163.     double w, z;                // width, style
  164.     get_token();    // "("
  165.     if(*token != '(')
  166.     {
  167.     serror(1);
  168.     return;
  169.     }
  170.     if(get_argument(&w) || get_argument(&z) || z < 0)
  171.     {
  172.     serror(0);
  173.     return;
  174.     }
  175.     KH_Paint::setlinestyle(w, z);
  176.     }
  177. //////////////////////
  178. //////////////////////
  179. void OOPic::Mirror()
  180.     {
  181.     if(error)
  182.     return;
  183.  
  184.     double z;
  185.     get_token();    // "("
  186.     if(*token != '(')
  187.     {
  188.     serror(1);
  189.     return;
  190.     }
  191.     if(get_argument(&z))
  192.     {
  193.     serror(0);
  194.     return;
  195.     }
  196.     set_mirror((int)z);
  197.     }
  198. /////////////////
  199. void OOPic::scroll()
  200.     {
  201.     if(error)
  202.     return;
  203.  
  204.     double x, y;
  205.     get_token();    // "("
  206.     if(*token != '(')
  207.     {
  208.     serror(1);
  209.     return;
  210.     }
  211.     if(get_argument(&x) || get_argument(&y))
  212.     {
  213.     serror(0);
  214.     return;
  215.     }
  216.     Paint::set_scroll((int)x, (int)y);
  217.     }
  218. //////////////////////
  219. void OOPic::add_Scroll()
  220.     {
  221.     if(error)
  222.     return;
  223.  
  224.     double x, y;
  225.     get_token();    // "("
  226.     if(*token != '(')
  227.     {
  228.     serror(1);
  229.     return;
  230.     }
  231.     if(get_argument(&x) || get_argument(&y))
  232.     {
  233.     serror(0);
  234.     return;
  235.     }
  236.     Paint::set_add_scroll((int)x, (int)y);
  237.     }
  238. //////////////////////
  239. void OOPic::Line() // LINE(X1, Y1, X2, Y2)
  240.     {
  241.     if(error)
  242.     return;
  243.  
  244.     double x1, y1, x2, y2;
  245.     get_token();    // "("
  246.     if(*token != '(')
  247.     {
  248.     serror(1);
  249.     return;
  250.     }
  251.     if(get_argument(&x1) || get_argument(&y1) || get_argument(&x2)
  252.        || get_argument(&y2))
  253.     {
  254.     serror(0);
  255.     return;
  256.     }
  257.  
  258.     line(x1, y1, x2, y2);
  259.     moveto(x2, y2);
  260.     }
  261. //////////////
  262. void OOPic::moveTo()
  263.     {
  264.     if(error)
  265.     return;
  266.  
  267.     double x, y;
  268.     get_token();    // "("
  269.     if(*token != '(')
  270.     {
  271.     serror(1);
  272.     return;
  273.     }
  274.     if(get_argument(&x) || get_argument(&y))
  275.     {
  276.     serror(0);
  277.     return;
  278.     }
  279.     moveto(x, y);
  280.     }
  281. /////////////////
  282. int OOPic::get_argument(double* x)
  283.     {
  284. /*    get_token();   // line argument
  285.     if(token_type != VARIABLE && token_type != NUMBER
  286.     && token_type != COMMAND && token[0] != '-')
  287.     return 1;
  288.     putback();
  289. */
  290.     get_exp(x);
  291.     return 0;
  292.     }
  293. ////////////////////
  294. void OOPic::setcolor()
  295.     {
  296.     if(error)
  297.     return;
  298.  
  299.     double col;
  300.     get_token();    // "("
  301.     if(*token != '(')
  302.     {
  303.     serror(1);
  304.     return;
  305.     }
  306.     if(get_argument(&col))
  307.     {
  308.     serror(2);
  309.     return;
  310.     }
  311.     KH_Paint::setcolor(col);
  312.     }
  313. /////////////////
  314. void OOPic::setcolor_RGB()
  315.     {
  316.     if(error)
  317.     return;
  318.  
  319.     double r,g,b;
  320.     get_token();    // "("
  321.     if(*token != '(')
  322.     {
  323.     serror(1);
  324.     return;
  325.     }
  326.     if(get_argument(&r) || get_argument(&g) || get_argument(&b))
  327.     {
  328.     serror(2);
  329.     return;
  330.     }
  331.     KH_Paint::setcolor(r,g,b);
  332.     }
  333. /////////////////
  334. void OOPic::setfillstyle()
  335.     {
  336.     if(error)
  337.     return;
  338.  
  339.     double fstyle, col;
  340.     get_token();    // "("
  341.     if(*token != '(')
  342.     {
  343.     serror(1);
  344.     return;
  345.     }
  346.     if(get_argument(&fstyle) || get_argument(&col))
  347.     {
  348.     serror(2);
  349.     return;
  350.     }
  351.     KH_Paint::setfillstyle(fstyle, (int)col);
  352.     }
  353. /////////////////
  354. void OOPic::setfont()
  355.     {
  356.     if(error)
  357.     return;
  358.  
  359.     char font[MAXPATH];
  360.     get_token();    // "("
  361.     if(*token != '(')
  362.     {
  363.     serror(1);
  364.     return;
  365.     }
  366.     get_token();
  367.     strcpy(font, token);
  368.     BGI_Font::load(font);
  369.  
  370.     if(kh_error_code)
  371.     {
  372.     serror(20);
  373.     return;
  374.     }
  375.     }
  376. /////////////////
  377. void OOPic::settextsize()
  378.     {
  379.     if(error)
  380.     return;
  381.  
  382.     double mx, dx, my,dy;
  383.     get_token();    // "("
  384.     if(*token != '(')
  385.     {
  386.     serror(1);
  387.     return;
  388.     }
  389.     if(get_argument(&mx) || get_argument(&dx) || get_argument(&my)
  390.     || get_argument(&dy))
  391.     {
  392.     serror(2);
  393.     return;
  394.     }
  395.     BGI_Font::setusercharsize(mx, dx, my, dy);
  396.     }
  397. /////////////////
  398. void OOPic::setfill()
  399.     {
  400.     if(error)
  401.     return;
  402.  
  403.     double f;
  404.     get_token();    // "("
  405.     if(*token != '(')
  406.     {
  407.     serror(1);
  408.     return;
  409.     }
  410.     if(get_argument(&f))
  411.     {
  412.     serror(2);
  413.     return;
  414.     }
  415.     fill = f;
  416.     }
  417. ////////////////////
  418. void OOPic::lineTo()
  419.     {
  420.     if(error)
  421.     return;
  422.  
  423.     double x, y;
  424.     get_token();    // "("
  425.     if(*token != '(')
  426.     {
  427.     serror(1);
  428.     return;
  429.     }
  430.     if(get_argument(&x) || get_argument(&y))
  431.     {
  432.     serror(2);
  433.     return;
  434.     }
  435.     lineto(x, y);
  436.     }
  437. ////////////////////
  438. void OOPic::Ellipse()
  439.     {
  440.     if(error)
  441.     return;
  442.  
  443.     double x, y, stangle, endangle, xradius, yradius;
  444.     get_token();    // "("
  445.     if(*token != '(')
  446.     {
  447.     serror(1);
  448.     return;
  449.     }
  450.     if(get_argument(&x) || get_argument(&y) || get_argument(&stangle)
  451.        || get_argument(&endangle) || get_argument(&xradius)
  452.        || get_argument(&yradius))
  453.     {
  454.     serror(2);
  455.     return;
  456.     }
  457.     ellipse(x, y, stangle, endangle, xradius, yradius);
  458.     }
  459. ////////////////////
  460. void OOPic::Rectangle()
  461.     {
  462.     if(error)
  463.     return;
  464.  
  465.     double x1, y1, x2, y2;
  466.     get_token();    // "("
  467.     if(*token != '(')
  468.     {
  469.     serror(1);
  470.     return;
  471.     }
  472.     if(get_argument(&x1) || get_argument(&y1) || get_argument(&x2)
  473.        || get_argument(&y2))
  474.     {
  475.     serror(2);
  476.     return;
  477.     }
  478.  
  479.     rectangle(x1, y1, x2, y2);
  480.     }
  481. ////////////////////
  482. void OOPic::Poly()
  483.     {
  484.     if(error)
  485.     return;
  486.  
  487.     get_token();    // "("
  488.     if(*token != '(')
  489.     {
  490.     serror(1);
  491.     return;
  492.     }
  493.  
  494.     double num_points;
  495.     if(get_argument(&num_points))
  496.     {
  497.     serror(2);
  498.     return;
  499.     }
  500.     int* points = new int[num_points * 2 + 1];
  501.     double work;
  502.     for(int i = 0; i < 2 * num_points; i++)
  503.     {
  504.     if(get_argument(&work))
  505.         {
  506.         delete points;
  507.         serror(15);
  508.         return;
  509.         }
  510.     points[i] = (int)work;
  511.     }
  512.  
  513.     if(fill)
  514.     fillpoly(num_points, points);
  515.     else
  516.     drawpoly(num_points, points);
  517.     delete points;
  518.     }
  519. ////////////////////
  520. void OOPic::text()
  521.     {
  522.     if(error)
  523.     return;
  524.  
  525.     double x, y;
  526.     char str[80];
  527.     get_token();    // "("
  528.     if(*token != '(')
  529.     {
  530.     serror(1);
  531.     return;
  532.     }
  533.     if(get_argument(&x) || get_argument(&y))
  534.     {
  535.     serror(2);
  536.     return;
  537.     }
  538.  
  539.     get_token();                     // '"...' or value of var
  540.     if(token_type == QUOTE)          // it is string
  541.     strcpy(str, token);
  542.     else                             // it is a = (real)b or a = (str)b
  543.     {
  544.     putback();
  545.     double value;
  546.     char* str_value = get_exp(&value);
  547.     switch(variable_type)
  548.         {
  549.         case STR :  strcpy(str, str_value); break;
  550.         default:
  551.         sprintf(str, "%f", value);
  552.         break;
  553.         }
  554.     }
  555.     BGI_Font::outtextxy(x, y, str);
  556.     }
  557. ////////////////////
  558. void OOPic::serror(int err)   // rewrite to obtain position of error
  559.     {
  560.     KH_Paint::setcolor(WHITE);
  561.     KH_Paint::setlinestyle(1, 0);
  562.     KH_Paint::setfillstyle(SOLID_FILL, BLUE);
  563.     KH_Paint::setusercharsize(1,1,1,1);
  564.     fill = ON;
  565.     BGI_Font::load("litt.chr");
  566.     KH_Paint::set_zoom(1,1);
  567.     KH_Paint::set_add_zoom(1,1);
  568.     KH_Paint::set_scroll(0,0);
  569.     KH_Paint::set_add_scroll(0,0);
  570.     KH_Paint::set_stack(OFF);
  571.  
  572.     rectangle(0, 0, 320, 80);
  573.  
  574.     char c = prog[0];                       // Get line number
  575.     prog[0] = '\0';
  576.     char ln[5];
  577.     itoa(nRet(program) + 1, ln, 10);
  578.     prog[0] = c;
  579.  
  580.     outtextxy(10, 15, "line: ");
  581.     outtextxy(10 + gettextwidth("line: "), 15, ln);
  582.     outtextxy(10, 30, "; file: ");
  583.     outtextxy(10 + gettextwidth("; file: "), 30, playstack[play_used]->prog);
  584.     outtextxy(10, 45, error_string[err]);
  585.     error = 1;
  586.     }
  587. //////////////////////////
  588. ///////////////////////// DOS Demo //////////////////////////////////////////
  589. /*
  590. #include <conio.h>     // only for interruption on key pressed **************
  591. class Demo : public OOPic
  592.     {
  593.     virtual void terminate();  // User-defined terminator (ESC and so on)
  594.     };
  595. /////////////
  596. void Demo::terminate()
  597.     {
  598.     if(kbhit())
  599.     {
  600.     getch();
  601.     if(kbhit())
  602.         getch();
  603.     serror(23);
  604.     }
  605.     }
  606.  
  607. void main()
  608.     {
  609.     int gdriver = DETECT, gmode;
  610.     initgraph(&gdriver, &gmode, "");
  611.  
  612.     Demo* basic = new Demo();
  613. //    basic->basic(basic->load_program("oopic.vec"));
  614.     char s[80] = "mirror(0,0)\r\nrotate(0,0,0)\r\nleft=10\r\ntop=10\r\nright=200\r\nbottom=200";
  615.     basic->basic(s);
  616.     char s1[80] = "color(15):style(1,7):header=\"Hello\"font(\"litt.chr\"):textsize(1,1,1,1)";
  617.     basic->basic(s1);
  618.     basic->basic(basic->load_program("window.vec"));
  619.     delete basic;
  620.  
  621.     closegraph();
  622.     }
  623. */
  624. /*
  625. ////////////////////////// GDI Windows Demo ////////////////////////
  626. // This example demonstrates output to OWL window. If necessary,
  627. // any other Windows library could be used.
  628.  
  629. #include <owl.h>
  630. class Demo : public OOPic
  631.     {
  632.     public:
  633.     void demo();
  634.     };
  635. ///////////////////
  636. void Demo::demo()
  637.     {
  638.     basic(load_program("oopic.vec"));
  639.     }
  640. class TMyApp : public TApplication
  641. {
  642. public:
  643.   TMyApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  644.     LPSTR lpCmdLine, int nCmdShow)
  645.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  646.   virtual void InitMainWindow();
  647. };
  648.  
  649. _CLASSDEF(TMyWindow)
  650. class TMyWindow : public TWindow, public Demo
  651. {
  652. public:
  653.   TMyWindow(PTWindowsObject AParent, LPSTR ATitle)
  654.     : TWindow(AParent, ATitle), Demo() { DC = GetDC(HWindow); }
  655.   virtual void WMLButtonDown(RTMessage Msg)
  656.     = [WM_FIRST + WM_LBUTTONDOWN];
  657. };
  658.  
  659. void TMyWindow::WMLButtonDown(RTMessage)
  660.     {
  661.     DC = GetDC(HWindow);
  662.     demo();
  663.     }
  664.  
  665. void TMyApp::InitMainWindow()
  666. {
  667.   MainWindow = new TMyWindow(NULL, Name);
  668. }
  669.  
  670. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  671.   LPSTR lpCmdLine, int nCmdShow)
  672. {
  673.   TMyApp MyApp("Sample KNOW-HOW.GRAPHICS Program", hInstance, hPrevInstance,
  674.            lpCmdLine, nCmdShow);
  675.   MyApp.Run();
  676.   return MyApp.Status;
  677. }
  678.  
  679. */